using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;

namespace ComputerShopE3.Controllers
{
    public class PowerController : Controller
    {
        //
        // GET: /Power/

        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();


        public ActionResult Index()
        {
            foreach (var item in _db.POWER)
            {
                item.SAN_PHAMReference.Load();
                item.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                item.SAN_PHAM.NHA_PHAN_PHOIReference.Load();
                item.SAN_PHAM.NHA_SAN_XUATReference.Load();
                item.SAN_PHAM.HINH_ANH.Load();
            }

            return View(_db.POWER.ToList());
        }

        //
        // GET: /Case/Details/5

        public ActionResult Details(int id)
        {
            var item = _db.POWER.Where((c) => c.id == id).FirstOrDefault();
            item.SAN_PHAMReference.Load();
            item.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
            item.SAN_PHAM.NHA_SAN_XUATReference.Load();
            item.SAN_PHAM.NHA_PHAN_PHOIReference.Load();
            item.SAN_PHAM.HINH_ANH.Load();

            ViewData["SAN_PHAM"] = item.SAN_PHAM;
            ViewData["GOP_Y"] = _db.GOP_Y.Where((c) => c.SAN_PHAM.id == id);

            return View(item);
        }

        //
        // GET: /Case/Create

        public ActionResult Create()
        {
            ViewData["NhaSanXuat"] = new SelectList(_db.NHA_SAN_XUAT.ToList(), "id", "TenNSX");
            ViewData["NhaPhanPhoi"] = new SelectList(_db.NHA_PHAN_PHOI.ToList(), "id", "TenNhaPhanPhoi");
            return View();
        }

        //
        // POST: /Case/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(
            [Bind(Exclude = "id", Prefix = "sp")]SAN_PHAM spToCreate,
            [Bind(Prefix = "linhkien")]POWER linhkienToCreate,
            FormCollection form)
        {
            short key = 0;
            try
            {
                int idNSX = int.Parse(form.Get("idNSX"));
                int idNhaPhanPhoi = int.Parse(form.Get("idNhaPhanPhoi"));
                spToCreate.NHA_PHAN_PHOI = _db.NHA_PHAN_PHOI.Where(npp => npp.id == idNhaPhanPhoi).First();
                spToCreate.NHA_SAN_XUAT = _db.NHA_SAN_XUAT.Where(nsx => nsx.id == idNSX).First();
                spToCreate.LOAI_SAN_PHAM = _db.LOAI_SAN_PHAM.Where(lsp => lsp.TenLoaiSanPham == "RAM").First();
                _db.AddToSAN_PHAM(spToCreate);
                _db.SaveChanges();

                key = _db.SAN_PHAM.Max(sp => sp.id);

                linhkienToCreate.id = key;
                linhkienToCreate.SAN_PHAM = _db.SAN_PHAM.Where(sp => sp.id == key).First();
                _db.AddToPOWER(linhkienToCreate);
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["NhaSanXuat"] = new SelectList(_db.NHA_SAN_XUAT.ToList(), "id", "TenNSX");
                ViewData["NhaPhanPhoi"] = new SelectList(_db.NHA_PHAN_PHOI.ToList(), "id", "TenNhaPhanPhoi");
                ViewData["error"] = ex.Message + "<br/>Product ID: " + key.ToString() + "<br/>";
                //ex.InnerException.Message;
                return View();
            }
        }

        //
        // GET: /Case/Edit/5

        public ActionResult Edit(int id)
        {
            POWER item = _db.POWER.Where(r => r.id == id).First();
            item.SAN_PHAMReference.Load();
            item.SAN_PHAM.NHA_PHAN_PHOIReference.Load();
            item.SAN_PHAM.NHA_SAN_XUATReference.Load();
            //if(item.SAN_PHAM.NHA_SAN_XUAT != null)  ViewData["NhaSanXuat"] = new SelectList(_db.NHA_SAN_XUAT.ToList(), "id", "TenNSX", item.SAN_PHAM.NHA_SAN_XUAT.id);
            //if (item.SAN_PHAM.NHA_PHAN_PHOI != null) ViewData["NhaPhanPhoi"] = new SelectList(_db.NHA_PHAN_PHOI.ToList(), "id", "TenNhaPhanPhoi", item.SAN_PHAM.NHA_PHAN_PHOI.id);

            return View(item);
        }

        //
        // POST: /Case/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(short id,
            [Bind(Prefix = "sp")]SAN_PHAM spToEdit,
            [Bind(Prefix = "linhkien")]RAM linhkienToEdit,
            FormCollection form)
        {
            try
            {
                SAN_PHAM originalSP = _db.SAN_PHAM.Where(sp => sp.id == id).First();
                POWER originalRam = _db.POWER.Where(r => r.id == id).First();

                short idNSX = short.Parse(form.Get("idNSX"));
                short idNhaPhanPhoi = short.Parse(form.Get("idNhaPhanPhoi"));

                //fix, current ids = 0.
                spToEdit.id = id;
                linhkienToEdit.id = id;

                //update foreign key.
                originalSP.NHA_SAN_XUATReference.EntityKey = new System.Data.EntityKey("ComputerShopDBEntities1.NHA_SAN_XUAT", "id", idNSX);
                originalSP.NHA_PHAN_PHOIReference.EntityKey = new System.Data.EntityKey("ComputerShopDBEntities1.NHA_PHAN_PHOI", "id", idNhaPhanPhoi);

                _db.ApplyPropertyChanges(originalSP.EntityKey.EntitySetName, spToEdit);
                //_db.SaveChanges();

                //update ram.
                long tienTrongNam = _db.HOA_DON.Where(a => a.NgayXacNhan.CompareTo(new DateTime(2009, 0, 0)) > 0).AsQueryable().Sum(hd2 => hd2.TongTien);

                _db.ApplyPropertyChanges(originalRam.EntityKey.EntitySetName, linhkienToEdit);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                POWER item = _db.POWER.Where(r => r.id == id).First();
                item.SAN_PHAMReference.Load();
                item.SAN_PHAM.NHA_PHAN_PHOIReference.Load();
                item.SAN_PHAM.NHA_SAN_XUATReference.Load();
                if(item.SAN_PHAM.NHA_SAN_XUAT != null)  ViewData["NhaSanXuat"] = new SelectList(_db.NHA_SAN_XUAT.ToList(), "id", "TenNSX", item.SAN_PHAM.NHA_SAN_XUAT.id);
                if (item.SAN_PHAM.NHA_PHAN_PHOI != null) ViewData["NhaPhanPhoi"] = new SelectList(_db.NHA_PHAN_PHOI.ToList(), "id", "TenNhaPhanPhoi", item.SAN_PHAM.NHA_PHAN_PHOI.id);
                ViewData["error"] = ex.Message;
                return View(item);
            }
        }   
    }
}
